;(function ($){
    var showo = function() {
        //	var colors = ["rgba(0,0,0,0.8)"];
        var addressdiv;
        var addbtn;
        var closebtn;
        var morephonebtn;
        var deletebtn;
        var savebtn;
        var addresswrapper;
        var addressinput;
        var sexsel;
        var showmoreaddress;
        var hideaddresswrapper;

        var loadEvent = function () {
            $(".ttishi").hover(function () {
//			var color = colors[n];
//  	$(".ttishi").css("background-color", color);
                $(".tishii").stop(true).show();
                $(".tishii").css("BackgroundColor: grey");
            }, function () {
                $(".tishii").stop(true).hide();
                $(".tishii").css("BackgroundColor: white");

            })
            $(".plus").on("click", adddd);
            $(".reduce").on("click", reduce);
            addbtn.on('click',showChangeInfo);
            closebtn.on('click',hideChangeInfo);
            morephonebtn.on('click',showMorePhone);
            deletebtn.on('click',hideMorePhone);
            savebtn.on('click',saveAddress);
            showmoreaddress.on('click',showMoreAdderss);
        }
        
        
        var adddd = function () {
            console.log("123");
            var t = $(this).parent().find('input[class*=text_box]');
            t.val(parseInt(t.val()) + 1)
            setTotal();
        }
        
        var reduce = function () {
            var t = $(this).parent().find('input[class*=text_box]');
            t.val(parseInt(t.val()) - 1)
            if (parseInt(t.val()) < 1) {
                $(this).parents('.listt').remove();
            }
            setTotal();
        }
        
        var setTotal = function () {
            $("#total1").html(0.00);//总价初始化
            var s = 0;//单价初始化
            var productList = $('.listt');//获取放商品的Div
            for(var i = 0; i < productList.length; i++){
                s +=  parseInt($(productList[i]).find('input').val()) * parseFloat($(productList[i]).find('.price').html().replace('￥',''));
            }//遍历商品，单价*数量
            s +=  -parseInt($('#newreduce').html().replace('￥-','')) + parseInt($('#exfee').html().replace('￥',''));//总价减去配送费和折扣
            //s += parseInt($(this).find('input[class*=text_box]').val()) * parseFloat($(this).find('p[class*=price]').text());
            $("#total1").html(s.toFixed(2));//把总价用两位数放到页面中
        }
        
        var showChangeInfo = function(){
			$('body').append('<div class="blackbox"></div>');
			addressdiv.show();
		}
		
		var hideChangeInfo = function(){
			for(var i = 0; i < addressinput.length - 1; i++){
				$(addressinput[i]).css('border-color','');
				$(addressinput[i]).parent().find('.addressformfield-hint span').removeClass('icon-dot-error').html('');
			}
			$('.blackbox').remove();
			addressdiv.hide();
		}
		
		var showMorePhone = function(){
			$('.addressformfield.phonebkfield').show();
		}
		
		var hideMorePhone = function(){
			$('.addressformfield.phonebkfield').hide();
		}
		
		var saveAddress = function(){
			for(var i = 0; i < addressinput.length - 1; i++){
				if(addressinput[i].value === ''){
					$(addressinput[i]).css('border-color','#ff464c');
					$(addressinput[i]).parent().find('.addressformfield-hint span').addClass('icon-dot-error').html('请输入正确的' + $(addressinput[i]).parent().find('label:eq(0)').html());
				}else{
					$(addressinput[i]).css('border-color','');
					$(addressinput[i]).parent().find('.addressformfield-hint span').removeClass('icon-dot-error').html('');
				}
			}
			if($('.icon-dot-error').length === 0){
				$(addresswrapper.find('p')[0]).html($(addressinput[0]).val() + (addressinput[1].checked ? ' 先生':' 女士') + $(addressinput[5]).val());
				$(addresswrapper.find('p')[1]).html($(addressinput[3]).val() + $(addressinput[4]).val());
				//ajax将数据传给后台
				addressinput.val('');
				hideChangeInfo();
			}
		}
		
		var showMoreAdderss = function(){
			if(this.isopen){
				hideaddresswrapper.hide();
				$(this).html('显示更多地址');
				$(this).next().removeClass('icon-arrow-up').addClass('icon-arrow-down');
				this.isopen = false;
			}else{
				hideaddresswrapper.show();
				$(this).html('收起');
				$(this).next().removeClass('icon-arrow-down').addClass('icon-arrow-up');
				this.isopen = true;
			}
			
		}

        return{
            init:function(){
            	addressdiv = $('.addressdialog');
            	addbtn = $('.addressadd');
            	closebtn = $('.addressdialog-close,.addressform-buttons button:last-of-type');
            	morephonebtn = $('.addressformfield.phonefield button');
            	deletebtn = $('.addressformfield.phonebkfield button');
            	savebtn = $('.addressdialog-close,.addressform-buttons button:first-of-type');
            	addresswrapper = $('.addresswrapper');
            	addressinput = $('.addressdialog input');
            	sexsel = $('input[name=sex]');
            	showmoreaddress = $('.showmoreaddress');
            	hideaddresswrapper = $('.hideaddress');
                loadEvent();
            }
        }


    }();
showo.init();
})(jQuery);
